package acs.bridge.dao;

import acs.bridge.model.RawTableMail;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * acs.dao
 * User: KuznetsovStV
 * Date: 07.04.13
 * Time: 16:01
 */
public class bm_mailDAOImpl {
    private String tableName;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }


    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    private JdbcTemplate jdbcTemplate;


    public void Insert(RawTableMail a_RawTableMail) {
        jdbcTemplate.update("INSERT INTO " + getTableName() + " (SEND_ADR,RECIP_ADR, COPY_ADR, MAIL_SUBJECT, SMTP_HOST, SMTP_USER,\n" +
                "SMTP_PASS, ATTACH_DATA, ATTACH_TYPE, ATTACH_NAME, ARCH_ENABLE,\n" +
                "ARCH_NAME, BCOPY_ADR, MAIL_BODY) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                new Object[]{a_RawTableMail.getSend_adr(), a_RawTableMail.getRecip_adr(), a_RawTableMail.getCopy_adr(), a_RawTableMail.getMail_subject(), a_RawTableMail.getSmtp_host(), a_RawTableMail.getSmtp_user(),
                        a_RawTableMail.getSmtp_pass(), a_RawTableMail.getAttach_data(), a_RawTableMail.getAttach_type(), a_RawTableMail.getAttach_name(), a_RawTableMail.getArch_enable(),
                        a_RawTableMail.getArch_name(), a_RawTableMail.getBcopy_adr(), a_RawTableMail.getMail_body()});
    }

    public RawTableMail Select(BigDecimal a_raw_id) {

        RawTableMail rawTableMail = new RawTableMail();

        List<Map> rows = getJdbcTemplate().queryForList("select send_adr, recip_adr,\n" +
                "       copy_adr, mail_subject,\n" +
                "       attach_data, attach_type, attach_name, arch_name,\n" +
                "       bcopy_adr, mail_body from " + getTableName() + " where id=?", new Object[] { a_raw_id });
        for (Map row : rows) {
            rawTableMail.setSend_adr((String) (row.get("SEND_ADR")));
            rawTableMail.setRecip_adr((String) (row.get("RECIP_ADR")));
            rawTableMail.setCopy_adr((String) (row.get("COPY_ADR")));
            rawTableMail.setMail_subject((String) (row.get("MAIL_SUBJECT")));
            rawTableMail.setAttach_data((byte[]) (row.get("ATTACH_DATA")));
            rawTableMail.setAttach_type((String) (row.get("ATTACH_TYPE")));
            rawTableMail.setAttach_name((String) (row.get("ATTACH_NAME")));
            rawTableMail.setArch_name((String) (row.get("ARCH_NAME")));
            rawTableMail.setBcopy_adr((String) (row.get("BCOPY_ADR")));
            rawTableMail.setMail_body((String) (row.get("MAIL_BODY")));
        }

        return rawTableMail;
    }


    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

     public String get_raw_table_name() {
         return (String) jdbcTemplate.execute(
                 new CallableStatementCreator() {
                     public CallableStatement createCallableStatement(Connection connection) throws SQLException {
                         CallableStatement callableStatement = connection.prepareCall("{? = call BRIDGE.PCK_BRIDGE.GET_RAW_TABLE_NAME()}");
                         callableStatement.registerOutParameter(1, Types.VARCHAR);
                         return callableStatement;
                     }
                 },
                 new CallableStatementCallback() {
                     public Object doInCallableStatement(CallableStatement callableStatement)
                             throws SQLException, DataAccessException {
                         callableStatement.execute();
                         return callableStatement.getString(1);
                     }


                 }
         );
     }

}